/* 
Copyright (C) 2010- Peter Bui and Aaron Dingler.
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "smq_common.h"
#include "smq_catalog.h"
#include "smq_client.h"
#include "smq_util.h"

#include <debug.h>

#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <nvpair.h>

#define SMQ_BIND_LARGE_FILE  1 << 20

static int  SMQ_Bind_RPC_Timeout        = SMQ_CLIENT_DEFAULT_RPC_TIMEOUT;
static int  SMQ_Bind_Connect_Timeout    = SMQ_CLIENT_DEFAULT_CONNECT_TIMEOUT;
static int  SMQ_Bind_Connect_Mode       = SMQ_CLIENT_CONNECT_FCFS;

static void
smq_bind_show_help(const char *progname)
{
    printf("usage: %s [options] [host:]queue script\n", progname);
    printf("Options:\n");
    printf(" -d <flag>   Enable debugging for this subsystem.\n");
    printf(" -t <time>   RPC timeout (default is %ds).\n", SMQ_Bind_RPC_Timeout);
    printf(" -r		 Select random host (default is FCFS).\n");
    printf(" -h          This message.\n");
}

static void
smq_bind_parse_command_line_arguments(int argc, char *argv[])
{
    int	c;

    while ((c = getopt(argc, argv, "d:t:rh")) != -1) {
        switch (c) {
            case 'd': 
                debug_flags_set(optarg); 
                break;
            case 't': 
                SMQ_Bind_RPC_Timeout = atoi(optarg);
                break;
            case 'r': 
		srand(time(0));
		SMQ_Bind_Connect_Mode = SMQ_CLIENT_CONNECT_RANDOM;
		break;
            case 'h':
                smq_bind_show_help(argv[0]); 
                exit(EXIT_SUCCESS);
                break;
            default:
                smq_bind_show_help(argv[0]); 
                exit(EXIT_FAILURE);
                break;
        }
    }
}

int
main(int argc, char *argv[])
{
    struct link *manager_link;
    char target[SMQ_LINE_MAX];
    char *script;
    FILE *script_stream;

    smq_bind_parse_command_line_arguments(argc, argv);

    if((argc - optind) != 2) {
        smq_bind_show_help("smq_bind");
        exit(EXIT_FAILURE);
    }

    strncpy(target, argv[optind], SMQ_LINE_MAX);
    script = argv[optind + 1];

    manager_link = smq_client_connect(target, 1, SMQ_Bind_Connect_Mode, SMQ_Bind_Connect_Timeout);
    if (!manager_link) 
	fatal("could not connect to queue %s\n", target);
    
    script_stream = fopen(script, "r");
    if (!script_stream)
	fatal("could not open script: %s", script);

    int result;
    if (smq_util_get_file_size(script) > SMQ_BIND_LARGE_FILE) {
        result = smq_client_bind_from_file(manager_link, target, time(0) + SMQ_Bind_RPC_Timeout, script_stream, basename(script));
    } else {
	char *script_buffer;
	int slen;

	/* TODO: error checking */
        fseek(script_stream, 0, SEEK_END);
        slen = ftell(script_stream);
        fseek(script_stream, 0, SEEK_SET);
        script_buffer = (char *)malloc(slen + 1);
        fread(script_buffer, slen, 1, script_stream);
        fclose(script_stream);
        result = smq_client_bind_from_buffer(manager_link, target, time(0) + SMQ_Bind_RPC_Timeout, script_buffer, basename(script), slen);
    }
    if (!result)
	fatal("put RPC failed: %d", result);
    return EXIT_SUCCESS;
}


/* 
vim: sw=4 sts=4 ts=8 ft=cpp
*/
